﻿using ApplicationBase.Core.Model;
using IdentityService.Application.Commands;
using IdentityService.Application.QueryService;
using IdentityService.Domain.Aggregates.RoleAggregate;
using IdentityService.Domain.Services;
using IdentityService.Infrastructure.Dtos;
using Infrastructure.Core.Web.Controller;
using Microsoft.AspNetCore.Mvc;

namespace IdentityService.WebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class RoleController : BaseController
    {
        private readonly IRoleQueryService _roleQueryService;
        public RoleController(IRoleQueryService roleQueryService)
            => _roleQueryService = roleQueryService;

        [HttpPost]
        public async Task<ActionResult<CommonResponseModel>> Create(string name, string appName, string description = "")
        {
            var result = await Mediator.Send(new CreateRoleCommand(name, appName, description));

            if (result.Result == CreateRoleResult.Success)
                return Ok(new CommonResponseModel());
            else
                return Ok(new CommonResponseModel(ResponseStatus.Failure, result.Result.ToString()));
        }

        [HttpDelete]
        public async Task<ActionResult<CommonResponseModel>> Delete(long id)
        {
            var result = await Mediator.Send(new DeleteRoleCommand(id));

            if (result == DeleteRoleResult.Success)
                return Ok(new CommonResponseModel());
            else
                return Ok(new CommonResponseModel(ResponseStatus.Failure, result.ToString()));
        }

        [HttpPut]
        public async Task<ActionResult<CommonResponseModel>> Modify(long id, string name, string description = "")
        {
            var result = await Mediator.Send(new ModifyRoleCommand(id, name, description));

            if (result == ModifyRoleResult.Success)
                return Ok(new CommonResponseModel());
            else
                return Ok(new CommonResponseModel(ResponseStatus.Failure, result.ToString()));
        }

        [HttpPut]
        public async Task<ActionResult<CommonResponseModel>> AddMemberBluk(long roleId, List<long> userAccountIds)
        {
            await Mediator.Send(new AddMemberCommand(roleId, userAccountIds));

            return Ok(new CommonResponseModel());
        }

        [HttpPut]
        public async Task<ActionResult<CommonResponseModel>> RemoveMemberBluk(long roleId, List<long> userAccountIds)
        {
            await Mediator.Send(new RemoveMemberCommand(roleId, userAccountIds));

            return Ok(new CommonResponseModel());
        }

        [HttpPut]
        public async Task<ActionResult<CommonResponseModel>> ChangePermits(long roleId, List<long> PermitIds)
        {
            var result = await Mediator.Send(new ChangePermitsCommand(roleId, PermitIds));

            if(result == ChangePermitsResult.Success) 
                return Ok(new CommonResponseModel());
            else
                return Ok(new CommonResponseModel(ResponseStatus.Failure, result.ToString()));
        }

        [HttpGet]
        public async Task<ActionResult<List<Role>>> GetAll()
        {
            return await _roleQueryService.GetAllAsync();
        }

        [HttpGet]
        public async Task<ActionResult<Role?>> GetDetail(long id)
        {
            return await _roleQueryService.GetDetailByIdAsync(id);
        }

        [HttpPost]
        public async Task<ActionResult<TableData<UserAccountDto>>> FindMemberOfRole([FromBody] FindMemberOfRoleQueryRequest request) 
        {
            return await _roleQueryService.FindMemberOfRoleAsync(request);
        }
    }
}
